package com.tater.controller;

import com.tater.client.NettyClient;
import com.tater.config.NettyConfig;
import com.tater.model.User;
import com.tater.proxy.RpcProxyFactory;
import com.tater.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
public class RpcController {

    RpcProxyFactory<IUserService> proxyFactory;


    @PostConstruct
    public void createProxy(){
        proxyFactory=new RpcProxyFactory<>(IUserService.class);
        System.out.println("连接服务器"+NettyConfig.NETTY_PORT);
    }



    @GetMapping("findUser")
    public String rpcTest(Integer userId){
        IUserService userService = null;
        try {
            userService = proxyFactory.getProxy();
            User user = userService.findByUserId(userId);
            if (user == null) {
                return "id="+userId+" 的用户不存在";
            }
            return user.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
}
